package domain

import (
	"album/dao"
	"album/model"
	"album/repo"
	"context"
	"errors"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
	"grpc-common/album/types/album"
	"listenboos-common/db"
)

type TrackStatDomain struct {
	trackStatRepo repo.AlbumStatRepo
}

func NewTrackStatDomain(db *db.ListenDB) *TrackStatDomain {
	return &TrackStatDomain{
		trackStatRepo: dao.NewTrackInfoDao(db),
	}
}
func (t *TrackStatDomain) SaveTrackStat(ctx context.Context, conn db.DBConn, trackId int64, in *album.SaveTrackReq) error {
	var data model.TrackStat
	err := copier.Copy(&data, &in)
	if err != nil {
		return err
	}
	data.TrackId = trackId
	err = t.trackStatRepo.SaveTrackStat(ctx, conn, &data)
	if err != nil {
		return err
	}
	return nil
}
func (t *TrackStatDomain) FindByTrackId(ctx context.Context, id int64) (playStatNum, collectStatNum, praiseStatNum, commentStatNum string, err error) {
	playStatNum, collectStatNum, praiseStatNum, commentStatNum, err = t.trackStatRepo.FindByTrackId(ctx, id)
	if err != nil {
		return "", "", "", "", err
	}
	return playStatNum, collectStatNum, praiseStatNum, commentStatNum, nil
}

func (t *TrackStatDomain) DeleteTrackStat(ctx context.Context, conn db.DBConn, trackId int64) error {
	err := t.trackStatRepo.DeleteTrackStat(ctx, conn, trackId)
	if err != nil {
		logx.Error(err)
		return errors.New("数据库异常")
	}

	return nil
}

func (t *TrackStatDomain) UpdateTrackNum(ctx context.Context, aid int64, tid int64, statType string, count int64) error {
	err := t.trackStatRepo.UpdateTrackNum(ctx, aid, tid, statType, count)
	if err != nil {
		logx.Error(err)
		return errors.New("数据库异常")
	}

	return nil
}
